/*
 * ================================================================================变量定义区
 */
var optype = ""; //定义操作类型为 create/update
var currentPage = 1;//全局变量，当前页
var passwordBefore ="";//密码未修改时不需要重复提交
												
/*
 * ================================================================================页面初始化函数
 */
$(function(){
	//init data
	var username = $("#hiddenUsername").val();
	if(username && username.length>0){
		optype = "update";
		$("#pageName").empty().append("update user");
		passwordBefore = $("#userForm input[name=password]").val();		
	}else{
		optype = "create";
		$("#pageName").empty().append("create user");
	}
	//绑定事件
	$("#btnSubmit").bind('click',function(){submitData();});
	$("#btnCancel").bind('click',function(){cancel();});	
});


/*
 * ================================================================================函数定义区
 * 
 */
/**
 * submit
 */
function submitData(){
	//add validation	
	
	/**
	 * validation可直接在Input属性中设置，也可在此设置规则
	 */
	var validateRules = {
		rules: {
			abc: "required" 
		} 
	};	
	var validateResult = $("#userForm").validate(validateRules).form();
	if(!validateResult){
		openAlert("请按提示填写完整信息");
		return;
	}else{
		var userinfo = getFormObj("userForm");	 
		if( userinfo.password ==passwordBefore ){
			delete userinfo.password;
		}
		var data = JSON.stringify(userinfo);
		 
		$.ajax({
			type: 'POST',
	        url: BASE_PATH +'/testUser/addOrUpdateUser.do?optype='+optype,
	        dataType:"json",       
	        contentType:"application/json;charset=UTF-8",
			data:data,
			success : function(data) {
				if(data=='success'){
					openAlert('保存成功',function(){
						window.location.href =BASE_PATH +'/testUser/findUsers.do';
					});
				}else{
					openAlert('保存失败');
				}
				
			},
			error:function(e){
				openAlert('操作失败！错误信息：<div>'+e.responseText+"</div>");
			}
		});
	}
}
/**
 * cancel button
 */
function cancel(){
	openConfirm('确定返回吗？', null, function() {
		window.location.href = BASE_PATH + '/testUser/findUsers.do';
	});
	
}

